//
//  ModifyInfoViewController.m
//  WeiWeiHaircut
//
//  Created by 超群 on 15/10/15.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "ModifyInfoViewController.h"
#import "ModifyCommonCell.h"
#import "ModifySwtchCell.h"
#import "UIView+RoundView.h"
#import "UIButton+WebCache.h"
#import "UIViewController+tool.h"
#import "QZEditNickNameVC.h"
#import "QZEditSignVC.h"
#import "QZEditPhoneNumVC.h"
#import "QZEditSexVC.h"
#import "QZCountyVc.h"
#import "QZSeleteTimeView.h"
#import "QZDatePickerViewController.h"
#import "QZEditBirthdayVc.h"
#import "UIImage+size.h"

@interface ModifyInfoViewController () <UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>
{
    NSArray* titleArray;
    NSArray * infoArray;
    UIImage *_headerIamgeSave;//上传的时候本地存储一下
}

@end

@implementation ModifyInfoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self titleWithSting:@"资料修改"];
    
    [self buildTableViewUI];
    [self addBtnOnNavWithTitle:@"资料" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    [self.headerView setCornerRadius:3];
    [self.headerView setBackgroundImage:[QZUserInfo headImage] forState:UIControlStateNormal];
    
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    infoArray = @[[QZUserInfo nickName],[QZUserInfo sex],[QZUserInfo address],[QZUserInfo userday],[QZUserInfo usertel], [QZUserInfo sign]];
    [_tableView reloadData];
}
-(void)loadData
{
    titleArray = @[@"昵称",@"性别",@"地区",@"生日",@"联系电话",@"签名"];
    infoArray = @[[QZUserInfo nickName],[QZUserInfo sex],[QZUserInfo address],[QZUserInfo userday],[QZUserInfo usertel], [QZUserInfo sign]];
    [_tableView reloadData];
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)buildTableViewUI
{
    self.tableView.delegate         = self;
    self.tableView.dataSource       = self;
    self.tableView.backgroundColor  = [UIColor groupTableViewBackgroundColor];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ModifyCommonCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ModifyCommonCell class])];
}

- (void)hidenKeyBoard
{
    [self.view endEditing:YES];
}

#pragma mark - tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *nextVc;
    if (indexPath.row == 0) {
        QZEditNickNameVC *editNickVc = [[QZEditNickNameVC alloc]init];
        nextVc = editNickVc;
    }
    if (indexPath.row == 1) {
        QZEditSexVC *editSexVc = [[QZEditSexVC alloc]init];
        nextVc = editSexVc;
    }
    if (indexPath.row == 2) {
        QZCountyVc *editAddressVc = [[QZCountyVc alloc]init];
        nextVc = editAddressVc;
    }
    if (indexPath.row == 3) {
        QZEditBirthdayVc *editBirthdayVc = [[QZEditBirthdayVc alloc]init];
        nextVc = editBirthdayVc;
    }
    if (indexPath.row == 4) {
        QZEditPhoneNumVC *editPhoneNumVc = [[QZEditPhoneNumVC alloc]init];
        nextVc = editPhoneNumVc;
    }
    if(indexPath.row == 5){
        QZEditSignVC *editSignVc = [[QZEditSignVC alloc]init];
        nextVc = editSignVc;
    }
    
    nextVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:nextVc animated:YES];
}

#pragma mark - tableView datasource
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModifyCommonCell * cell  = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ModifyCommonCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.texteLabel.text = infoArray[indexPath.row];
    return cell;
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            switch (buttonIndex) {
                case 0:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                    
                case 1:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    // 取消
                    return;
            }
        }
        else {
            if (buttonIndex == 1) {
                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        [self createImagePickerControllerWithSourceType:sourceType];
    }
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"User.GetSetPhoto"];
    //压缩图片
    UIImage *newImage = [image imageScaledToSize:CGSizeMake(319, 213)];
    _headerIamgeSave = newImage;
    NSData *imageData = UIImageJPEGRepresentation(newImage,0.5);//转二进制
    
    [self.request QZRequest_Upload_POST:urlStr parameters:@{@"user_id":[QZUserInfo userId],@"user_photo":@"heardImage"} imageData:imageData pathKey:@"heardImage" tagNSString:@"update-header" stopRequest:YES isSerializer:NO];
    
    [_headerView setBackgroundImage:image forState:UIControlStateNormal];
}
#pragma mark requestProol
-(void)request_Upload_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{
    
    if ([tag isEqualToString:@"update-header"]) {
        if ([value[@"data"][@"status"] integerValue] == 100) {
            [[iToast makeText:@"更新成功"] show];
            [[TMCache sharedCache] setObject:_headerIamgeSave forKey:@"headImage"];
        }else{
            [[iToast makeText:@"更新失败"] show];
        }
    
    }
  
    
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)headAction:(id)sender {
    //修改头像
    
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
        
    }else {
        
        sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", nil];
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}
@end
